﻿using System;
using System.Windows.Forms;
using Bonesoft.Forms.Contabil;
using Bonesoft.Forms.Contabil.FormulariosRelacionados;
using Business;
using Business.Entity.Contabil;
using Business.Entity.Contabil.ClassesRelacoinadas;
using Business.Entity.Contabil.Pessoa.SubClass.Usuario.ClassesRelacionadas;
using Business.Forms;
using Business.Seguranca;
using Business.Validation;
using DevExpress.XtraEditors;

namespace Bonesoft.Lists.Contabil
{
    public partial class ListCustosFixos : ListDefault,IForm
    {
        public ListCustosFixos()
        {
            InitializeComponent();
            FormEdit = new FormCustosFixos();
            Reload();
        }

        private void cmdGerarMes_Click(object sender, System.EventArgs e)
        {
            try
            {
                MesGerado m = new FormMesGerado().SolicitarMes();
                if (m == null)
                {
                    return;
                }

                using (var s = CustoFixoRepository.SessionFactory.OpenSession())
                {
                    using (var t = s.BeginTransaction())
                    {
                        using (var r = new CustoFixoRepository())
                        {
                            foreach (var c in custoFixoBindingSource)
                            {
                                r.GerarTitulo(s, (CustoFixo) c, m);
                            }
                        }
                        s.Save(m);
                        t.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Erro ao gerar mês.\n Erro encontrado:" + ex.Message);
                throw;
            }
            

        }

        public object Save()
        {
            throw new NotImplementedException();
        }

        public void Update(object entity)
        {
            FormEdit.Update(entity);
        }

        public bool Delete(object entity)
        {
            FormEdit.Delete(entity);
            return false;
        }

        public object CurrentObject
        {
            get
            {
                return custoFixoBindingSource.Current;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public PermissaoForm Permissao
        {
            get
            {
                return SegurancaUtils.GetPermissaoForm(Name);
            }
        }

        public EntityValidationResult Validate()
        {
            throw new NotImplementedException();
        }

        public void Reload()
        {
            custoFixoBindingSource.DataSource = CustoFixoRepository.GetList();
        }

        public void Cancel()
        {
            throw new NotImplementedException();
        }

        public object New()
        {
            var c = FormEdit.New();
            if (c != null)
            {
                custoFixoBindingSource.Add(c);
            }
            return c;
        }

        public XtraForm Form
        {
            get
            {
                return this;
            }
        }

        public FormState FormState { get; set; }
    }
}